package com.dg11185.nearshop.view.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.nearshop.R;
import com.dg11185.libs.imageloader.core.DisplayImageOptions;
import com.dg11185.libs.imageloader.core.ImageLoader;

/**
 * 首页上精选商家列表适配器
 * 
 * @author wanliang527
 * @date
 */
public class F_ShopListAdapter extends BaseAdapter {
    protected Context mContext;
    protected List<Map<String, Object>> mData;

    protected DisplayImageOptions options = new DisplayImageOptions.Builder()
            .showStubImage(R.drawable.web_image_background).cacheInMemory()
            .cacheOnDisc().showImageOnFail(R.drawable.web_image_background)
            .showImageForEmptyUri(R.drawable.web_image_background).build();

    public static class ViewHolder {
        public ImageView cover;
        public TextView name;
        public TextView description;
        public String brandId;
    }

    public F_ShopListAdapter(Context context, List<Map<String, Object>> data) {
        super();
        mContext = context;
        mData = data;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder vHolder = null;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.view_fshop_list_item, null);

            vHolder = new ViewHolder();
            vHolder.cover = (ImageView) convertView
                    .findViewById(R.id.image_v_shop_cover);
            vHolder.name = (TextView) convertView
                    .findViewById(R.id.txt_v_shop_name);
            vHolder.description = (TextView) convertView
                    .findViewById(R.id.txt_v_shop_desc);
            convertView.setTag(vHolder);
        }

        vHolder = (ViewHolder) convertView.getTag();
        Map<String, Object> singleData = mData.get(position);
        vHolder.brandId = (String) singleData.get("brandId");

        Object obj1 = singleData.get("coverUrl");
        if (obj1 != null && obj1 instanceof String)
            ImageLoader.getInstance().displayImage((String) obj1,
                    vHolder.cover, options);

        Object obj2 = singleData.get("name");
        if (obj2 != null && obj2 instanceof String)
            vHolder.name.setText((String) obj2);

        Object obj3 = singleData.get("description");
        if (obj3 != null && obj3 instanceof String)
            vHolder.description.setText((String) obj3);

        return convertView;
    }

    public List<Map<String, Object>> getData() {
        return mData;
    }
}
